package com.kaba.security.handler;

import com.alibaba.fastjson.JSON;
import com.kaba.bean.ResponseResult;
import com.kaba.bean.ResultCode;
import com.kaba.bean.ResultTool;
import com.kaba.bean.json.JsonResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author lvmengjie
 * @Date 2021/3/24
 * @Description 权限拒绝处理逻辑
 */
@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {

        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        ResponseResult result = ResponseResult.error(ResultCode.PERMISSION_DENIED);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(result));



    }
}
